﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;

namespace Kaleidoscope
{
    public partial class DrawImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DrawImage2();
        }

        private void DrawImage2()
        {
            int height = 100;
            int width = 100;
            //Random r = new Random();
            //int x = r.Next(75);

            byte[] uploadedImage = Session["uploadedFile"] as byte[];
            if (uploadedImage == null || uploadedImage.Length == 0)
            {
                Bitmap bmp = new Bitmap(width, height);//, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bmp);

                g.Clear(Color.Orange);
                g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3);
                g.DrawRectangle(Pens.Gray, 2, 2, width - 3, height - 3);
                g.DrawRectangle(Pens.Black, 0, 0, width, height);
                Pen blackPen = new Pen(Brushes.Black);
                g.DrawLine(blackPen, 0, 0, width, height);
                g.DrawLine(blackPen, 0, height, width, 0);
                bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
                g.Dispose();
                bmp.Dispose();
            }
            else
            {
                MemoryStream ms = new MemoryStream(uploadedImage);
                Bitmap bmp = null;
                try
                {
                    Bitmap bmpOrig = new Bitmap(ms);
                    Size size = bmpOrig.Size;
                    bmp = new Bitmap(size.Width, size.Height);//, PixelFormat.Format24bppRgb);
                    Graphics g = Graphics.FromImage(bmp);
                    g.Clear(Color.Black);
                    object grades = this.Session["gradesToRotate"];
                    if (grades != null && grades is float)
                    {
                        Matrix m = new Matrix();
                        //m.Scale(1, 1);
                        //m.Translate(0, size.Height);
                        bmpOrig.RotateFlip(RotateFlipType.RotateNoneFlipY);
                        m.RotateAt((float)grades,new PointF(size.Width / 2.0F, size.Height / 2.0F));
                        g.Transform=m;
                    }
                    g.DrawImage(bmpOrig, 0, 0);
                    bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
                    g.Dispose();
                }
                catch (ArgumentException)
                {
                    bmp = new Bitmap(width, height);//, PixelFormat.Format24bppRgb);
                    Graphics g = Graphics.FromImage(bmp);

                    g.Clear(Color.Orange);
                    Rectangle rect = new Rectangle(0, 0, width, height);
                    g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3);
                    g.DrawRectangle(Pens.Gray, 2, 2, width - 3, height - 3);
                    g.DrawRectangle(Pens.Black,rect);
                    Pen blackPen = new Pen(Brushes.Black);
                    Font font = new Font(FontFamily.GenericMonospace,140);
                    StringFormat sf = new StringFormat();
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    g.DrawString("Bad Image!", font, Brushes.Red,rect,sf);
                    //g.DrawLine(blackPen, 0, 0, width, height);
                    //g.DrawLine(blackPen, 0, height, width, 0);
                    bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
                    g.Dispose();
                    

                }
                finally
                {
                if (bmp!=null) bmp.Dispose();
                ms.Close();
                }
            }

            //Response.ContentType = "image/jpeg";

        }
    }
}
